package net.huashitong.supervision.task.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class HolidayUtils {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
    public static SimpleDateFormat sdf1= new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm:ss");

    /**
     * 获取计划激活日期
     *
     * @param today opening date
     * @param list  holidayList
     * @param num   num个工作日后
     * @return
     * @throws ParseException
     */
    public static Date getScheduleActiveDate(Date today, List<String> list, int num) throws ParseException {
        //节假日
        if (list.size() == 0) {
            list.add("2019.01.01");
            list.add("2019.02.04");
            list.add("2019.02.05");
            list.add("2019.02.06");
            list.add("2019.02.07");
            list.add("2019.02.08");
            list.add("2019.04.05");
            list.add("2019.05.01");
            list.add("2019.05.02");
            list.add("2019.05.03");
            list.add("2019.06.07");
            list.add("2019.09.13");
            list.add("2019.10.01");
            list.add("2019.10.02");
            list.add("2019.10.03");
            list.add("2019.10.04");
            list.add("2019.10.07");
        }

        Date tomorrow = null;
        int delay = 1;
        while (delay <= num) {
            tomorrow = getTomorrow(today);
            //当前日期+1,判断是否是节假日,不是的同时要判断是否是周末,都不是则scheduleActiveDate日期+1
            if (!isWeekend(sdf.format(tomorrow)) && isHoliday(sdf.format(tomorrow), list)) {
                today = tomorrow;
            } else if (isWeekend(sdf.format(tomorrow))) {
                //补班
                if (("2019.02.02").equals(sdf.format(tomorrow)) ||
                        ("2019.02.03").equals(sdf.format(tomorrow)) ||
                        ("2019.02.02").equals(sdf.format(tomorrow)) ||
                        ("2019.04.28").equals(sdf.format(tomorrow)) ||
                        ("2019.05.05").equals(sdf.format(tomorrow)) ||
                        ("2019.09.29").equals(sdf.format(tomorrow)) ||
                        ("2019.10.12").equals(sdf.format(tomorrow))
                        ) {
                    delay++;
                    today = tomorrow;
                } else {
                    today = tomorrow;
                }
            } else if (isHoliday(sdf.format(tomorrow), list)) {
                today = tomorrow;
            } else {
                delay++;
                today = tomorrow;
            }
        }

        return today;
    }

    /**
     * 获取tomorrow的日期
     *
     * @param date
     * @return
     */
    public static Date getTomorrow(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        date = calendar.getTime();
        return date;
    }

    /**
     * 判断是否是weekend
     *
     * @param sdate
     * @return
     * @throws ParseException
     */
    public static boolean isWeekend(String sdate) throws ParseException {
        Date date = sdf.parse(sdate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断是否是holiday
     *
     * @param sdate
     * @param list
     * @return
     * @throws ParseException
     */
    public static boolean isHoliday(String sdate, List<String> list) throws ParseException {
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (sdate.equals(list.get(i))) {
                    return true;
                }
            }
        }
        return false;
    }


    public static double calLeaveDays(Date startTime, Date endTime) {
        double leaveDays = 0;
        //从startTime开始循环，若该日期不是节假日或者不是周六日则请假天数+1
        Date flag = startTime;//设置循环开始日期
        Calendar cal = Calendar.getInstance();
        //循环遍历每个日期
        while (flag.compareTo(endTime) != 1) {
            cal.setTime(flag);
            //判断是否为周六日
            int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (week == 0 || week == 6) {//0为周日，6为周六
                //跳出循环进入下一个日期
                cal.add(Calendar.DAY_OF_MONTH, +1);
                flag = cal.getTime();
                continue;
            }
            //判断是否为节假日

            //不是节假日或者周末，天数+1
            leaveDays = leaveDays + 1;
            //日期往后加一天
            cal.add(Calendar.DAY_OF_MONTH, +1);
            flag = cal.getTime();
        }
        return leaveDays;
    }
}
